import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JList;
import java.awt.Color;
import java.util.Vector;

import javax.swing.border.BevelBorder;
import javax.swing.JTable;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import java.awt.Font;


public class EIHitzorduKudeatzailea extends JFrame {

	private JPanel contentPane;
	private JTextField nanSartu;
	private JTextField dataSartu;
	private JButton bilatu1;
	private JButton bilatu2;
	private JButton atzera;
	private JButton logout;
	private Taula taula;
	private JTextField dataGehitu;
	private JTextField orduaGehitu;
	private JTextField nanGehitu;
	private JComboBox terapiak;
	private JComboBox terapeutak;
	private JButton gehitu;
	private JButton ezabatu;
	private JTextField orduaSartu ;
	private JTextArea textArea;
	
	public EIHitzorduKudeatzailea () {
		elementuakHasieratu();
		botoiakHasieratu();
		setVisible(true);
	}
	
	public void elementuakHasieratu(){
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 900, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblHitzorduak = new JLabel("HITZORDUAK");
		lblHitzorduak.setBounds(12, 12, 100, 15);
		contentPane.add(lblHitzorduak);
		
		JLabel lblNanBidezBilatu = new JLabel("NAN:");
		lblNanBidezBilatu.setBounds(64, 39, 70, 17);
		contentPane.add(lblNanBidezBilatu);
		
		JLabel lblDataBidezBilatu = new JLabel("Data:");
		lblDataBidezBilatu.setBounds(64, 79, 62, 21);
		contentPane.add(lblDataBidezBilatu);
		
		nanSartu = new JTextField();
		nanSartu.setBounds(131, 38, 114, 19);
		contentPane.add(nanSartu);
		nanSartu.setColumns(10);
		
		dataSartu = new JTextField();
		dataSartu.setBounds(131, 80, 114, 19);
		contentPane.add(dataSartu);
		dataSartu.setColumns(10);
		
		bilatu1 = new JButton("NAN bidez bilatu");
		bilatu1.setBounds(266, 34, 182, 25);
		contentPane.add(bilatu1);
		
		bilatu2 = new JButton("Data bidez bilatu");
		bilatu2.setBounds(266, 75, 182, 25);
		contentPane.add(bilatu2);
		
		logout = new JButton("Logout");
		logout.setBounds(789, 7, 91, 25);
		contentPane.add(logout);
		
		atzera = new JButton("Atzera");
		atzera.setBounds(789, 535, 91, 25);
		contentPane.add(atzera);
		
		Vector<String> izenak = new Vector<String>();
		izenak.addElement("NAN");
		izenak.addElement("Terapeuta");
		izenak.addElement("Data");
		izenak.addElement("Ordua");
		izenak.addElement("Terapia");
		taula = new Taula(izenak);
		taula.setBounds(12, 161, 500, 399);
		contentPane.add(taula);
		
		JLabel lblHitzorduaGehitu = new JLabel("Hitzordua gehitu");
		lblHitzorduaGehitu.setBounds(592, 147, 176, 15);
		contentPane.add(lblHitzorduaGehitu);
		
		ezabatu = new JButton("Hitzordua ezabatu");
		ezabatu.setBounds(266, 124, 182, 25);
		contentPane.add(ezabatu);
		
		JLabel lblData = new JLabel("Data:");
		lblData.setBounds(530, 191, 70, 15);
		contentPane.add(lblData);
		
		JLabel lblOrdua = new JLabel("Ordua:");
		lblOrdua.setBounds(530, 236, 70, 15);
		contentPane.add(lblOrdua);
		
		JLabel lblTerapia = new JLabel("Terapia:");
		lblTerapia.setBounds(530, 332, 70, 15);
		contentPane.add(lblTerapia);
		
		JLabel lblTerapeuta = new JLabel("Terapeuta:");
		lblTerapeuta.setBounds(530, 388, 84, 15);
		contentPane.add(lblTerapeuta);
		
		JLabel lblBezeroa = new JLabel("Bezeroa:");
		lblBezeroa.setBounds(530, 278, 70, 15);
		contentPane.add(lblBezeroa);
		
		dataGehitu = new JTextField();
		dataGehitu.setBounds(615, 189, 199, 19);
		contentPane.add(dataGehitu);
		dataGehitu.setColumns(10);
		
		orduaGehitu = new JTextField();
		orduaGehitu.setBounds(615, 234, 199, 19);
		contentPane.add(orduaGehitu);
		orduaGehitu.setColumns(10);
		
		Vector<String> espezialitateLista= HitzorduKudeatzailea.getInstantzia().getEspezialitateak();
		terapiak = new JComboBox(espezialitateLista);
		terapiak.setBounds(615, 327, 199, 24);
		terapiak.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				terapeutakBete((String) terapiak.getSelectedItem());
			}

		});
		contentPane.add(terapiak);
		
		
		terapeutak = new JComboBox();
		terapeutak.setBounds(615, 383, 199, 24);
		terapeutakBete((String) terapiak.getSelectedItem());
		contentPane.add(terapeutak);
		
		
		nanGehitu = new JTextField();
		nanGehitu.setBounds(615, 276, 199, 19);
		contentPane.add(nanGehitu);
		nanGehitu.setColumns(10);
		
		gehitu = new JButton("Gehitu");
		gehitu.setBounds(615, 443, 117, 25);
		contentPane.add(gehitu);
		
		orduaSartu = new JTextField();
		orduaSartu .setBounds(131, 130, 114, 19);
		contentPane.add(orduaSartu );
		orduaSartu.setColumns(10);
		
		JLabel lblOrdua_1 = new JLabel("Ordua:");
		lblOrdua_1.setBounds(64, 129, 70, 15);
		contentPane.add(lblOrdua_1);
		
		textArea = new JTextArea();
		textArea.setForeground(new Color(51, 51, 51));
		textArea.setFont(new Font("Dialog", Font.PLAIN, 12));
		textArea.setBounds(559, 61, 219, 36);
		contentPane.add(textArea);
	}
	
	public void terapeutakBete(String esp){
		terapeutak.removeAllItems();
		Vector<String> lista= HitzorduKudeatzailea.getInstantzia().getTerapeutak(esp);
		
		for (String t : lista){
			terapeutak.addItem(t);
		}
		
		terapeutak.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				String aukera = (String) terapeutak.getSelectedItem();
			}

		});
	}
	
	public void botoiakHasieratu(){
		
		bilatu1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				Vector<Hitzordua> hitzorduak = HitzorduKudeatzailea.getInstantzia().hitzorduaNanBidezBilatu(nanSartu.getText());
				pantailaratu(hitzorduak);
			}
		});
		
		bilatu2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Vector<Hitzordua> hitzorduak = HitzorduKudeatzailea.getInstantzia().hitzoduaDataBidezBilatu(dataSartu.getText());
				pantailaratu(hitzorduak);
			}
		});
		
		atzera.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (EILogin.administratzailea){
					EIAdministratzailea frame = new EIAdministratzailea();	
				}else{
					EIIdazkari frame = new EIIdazkari();	
				}
				EIHitzorduKudeatzailea.this.dispose();
			}
		});
		
		logout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				EILogin frame = new EILogin();
				EIHitzorduKudeatzailea.this.dispose();
			}
		});
		
		ezabatu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String nan = nanSartu.getText();
				String data = dataSartu.getText();
				String ordua = orduaSartu.getText();
				Boolean burutua = HitzorduKudeatzailea.getInstantzia().hitzorduaEzabatu(nan,data,ordua);
				if (burutua){
					textArea.setText("Hitzordua ezabatu da.");
				}else{
					textArea.setText("Ez da hitzordua existitzen.");
				}
			
			}
		});
		
		gehitu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String nan = nanGehitu.getText();
				String data = dataGehitu.getText();
				String ordua = orduaGehitu.getText();
				String terapia = (String) terapiak.getSelectedItem();
				String terapeuta = (String) terapeutak.getSelectedItem();
				Hitzordua h = new Hitzordua(nan, terapeuta, data, ordua, terapia);
				Boolean burutua = HitzorduKudeatzailea.getInstantzia().hitzorduaGehitu(h);
				if (burutua){
					textArea.setText("Hitzordua gehitu da.");
				}else{
					textArea.setText("Ez da hitzordurik gehitu.");
				}
			}
		});
	
	}
	
	public void pantailaratu(Vector<Hitzordua> hitzorduLista){
		
		
		for (Hitzordua h: hitzorduLista){

			taula.lerroaGehitu(h);
			
		}
	}
}
